package com.kexilo.system.audit.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kexilo.core.audit.domain.SysArchiveConfig;
import com.kexilo.core.common.utils.StringUtils;
import com.kexilo.system.audit.mapper.SysArchiveConfigMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据归档配置服务实现
 *
 * @author Kexilo
 */
@Service
public class SysArchiveConfigServiceImpl implements ISysArchiveConfigService {
    
    private static final Logger log = LoggerFactory.getLogger(SysArchiveConfigServiceImpl.class);

    @Autowired
    private SysArchiveConfigMapper configMapper;

    @Override
    public List<SysArchiveConfig> selectConfigList(SysArchiveConfig config) {
        return configMapper.selectConfigList(config);
    }

    @Override
    public SysArchiveConfig selectConfigById(Long configId) {
        return configMapper.selectById(configId);
    }

    @Override
    @Transactional
    public int insertConfig(SysArchiveConfig config) {
        return configMapper.insert(config);
    }

    @Override
    @Transactional
    public int updateConfig(SysArchiveConfig config) {
        return configMapper.updateById(config);
    }

    @Override
    @Transactional
    public int deleteConfigByIds(Long[] configIds) {
        int count = 0;
        for (Long configId : configIds) {
            count += configMapper.deleteById(configId);
        }
        log.info("删除归档配置：数量={}", count);
        return count;
    }

    @Override
    @Transactional
    public int updateConfigStatus(Long[] configIds, String status) {
        return configMapper.updateStatusBatch(configIds, status);
    }

    @Override
    public List<SysArchiveConfig> selectEnabledConfigs() {
        return configMapper.selectEnabledConfigs();
    }

    @Override
    public SysArchiveConfig selectByTableName(String tableName) {
        return configMapper.selectByTableName(tableName);
    }

    @Override
    public String checkTableNameUnique(SysArchiveConfig config) {
        Long configId = config.getConfigId() == null ? -1L : config.getConfigId();
        int count = configMapper.checkTableNameExists(config.getTableName(), configId);
        if (count > 0) {
            return "表名已存在";
        }
        return "0"; // 唯一
    }

    @Override
    public String checkConfigNameUnique(SysArchiveConfig config) {
        Long configId = config.getConfigId() == null ? -1L : config.getConfigId();
        
        LambdaQueryWrapper<SysArchiveConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysArchiveConfig::getConfigName, config.getConfigName());
        wrapper.ne(configId != -1L, SysArchiveConfig::getConfigId, configId);
        
        long count = configMapper.selectCount(wrapper);
        if (count > 0) {
            return "配置名称已存在";
        }
        return "0"; // 唯一
    }
}
